/**
 * Created by rockyren on 15/1/11.
 */
describe('Unit: address控制器', function(){
  beforeEach(module('addressController'));
  var $httpBackend, $controller, $rootScope;
  beforeEach(inject(function(_$httpBackend_, _$controller_, _$rootScope_){
    $httpBackend = _$httpBackend_;
    $controller = _$controller_;
    $rootScope = _$rootScope_;
  }));

  describe('addressController', function(){
    var addressController, scope;
    var baseAddressUrl = 'api/personal/address';
    //回应输入
    var resAddress = {
      addressList:[
      {
        id: 10,
        name: 'Rocky',
        location: 'all over the earth',
        phone: '12345678910',
        postcode: '590000',
        default: true,
        district: {
          province: {
            id: 1,
            name: 'guangdong'
          },
          city: {
            id: 10,
            name: 'guangzhou'
          },
          region: {
            id: 100,
            name: 'tianhe'
          }
        }
      }
    ]};

    //创建一个模拟modal对象
    var fakeModal = {
      result: {
        then: function(confirmCallback,cancelCallback) {
          this.confirmCallback = confirmCallback;
          this.cancelCallback = cancelCallback;
        }
      },
      close: function(item) {
        this.result.confirmCallback(item);
      },
      dismiss: function(type) {
        this.result.cancelCallback(type);
      }
    };

    beforeEach(inject(function($modal){
      //$modal监听
      spyOn($modal,'open').andReturn(fakeModal);
    }));

    beforeEach(inject(function(_$modal_){
      scope = $rootScope.$new();

      addressController = $controller('addressController', {
        $scope: scope,
        $modal: _$modal_
      });

      //第一次刚初始化控制器时的请求
      $httpBackend.expectGET(baseAddressUrl).respond(200, resAddress);
      $httpBackend.flush();
    }));

    it('加载控制器后,能获得收货地址列表',function(){
      expect(scope.addressList).toEqual(resAddress.addressList);
    });

    it('调用createAddress后,触发close后,应该能请求创建一个地址',function(){

      scope.createAddress();

      var addressInfo = {
        name: 'Rocky',
        district: 'diqu',
        location: 'university',
        phone: '12345678910',
        postcode: 590000
      };

      $httpBackend.expectPOST(baseAddressUrl,addressInfo).respond(200, {});
      $httpBackend.expectGET(baseAddressUrl).respond(200,resAddress);
      scope.modalInstance.close(addressInfo);
      $httpBackend.flush();
    });

    it('调用editAddress后,触发close后,应该能请求修改一个地址',function(){
      scope.editAddress(12);
      var addressInfo = {
        name: 'Rocky',
        district: 'diqu',
        location: 'university',
        phone: '12345678910',
        postcode: 590000
      };

      $httpBackend.expectPUT(baseAddressUrl + '/12',addressInfo).respond(200, {});
      $httpBackend.expectGET(baseAddressUrl).respond(200,resAddress);
      scope.modalInstance.close(addressInfo);
      $httpBackend.flush();
    });

  });

  describe('addressModalController', function(){
    var addressModalController, scope, modalInstance;
    beforeEach(function(){
      scope = $rootScope.$new();

      //创建一个虚拟的modalInstance实例
      modalInstance = {
        close: jasmine.createSpy('modalInstance.close'),
        dismiss: jasmine.createSpy('modalInstance.dismiss'),
        result: {
          then: jasmine.createSpy('modalInstance.result.then')
        }
      };

      addressModalController = $controller('addressModalController', {
        $scope: scope,
        $modalInstance: modalInstance,
        addressList: null
      });
    });

    it('测试districtListHandle',function(){
      var resPro = [
        {
          id: 1,
          name: 'guangdong'
        },
        {
          id: 2,
          name: 'shanghai'
        }
      ];

      var resCity = [
        {
          id: 12,
          name: 'guangzhou'
        },
        {
          id: 22,
          name: 'shanghai'
        }
      ];

      var resReg = [
        {
          id: 121,
          name: 'huadu'
        },
        {
          id: 221,
          name: 'modu'
        }
      ];

      $httpBackend.expectGET('api/district').respond(200,resPro);
      //scope.districtListHandle.setProvinceList();
      $httpBackend.flush();
      expect(scope.districtList.provinceList).toEqual(resPro);


      $httpBackend.expectGET('api/district/123').respond(200,resCity);
      scope.districtListHandle.setCityList(123);
      $httpBackend.flush();
      expect(scope.districtList.cityList).toEqual(resCity);

      $httpBackend.expectGET('api/district/123/321').respond(200,resReg);
      scope.districtListHandle.setRegionList(123,321);
      $httpBackend.flush();
      expect(scope.districtList.regionList).toEqual(resReg);

    });
  });
});